package com.jiubanqingchen.org.user;

import com.jfinal.aop.Aop;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.jiubanqingchen.framework.annotation.HideColumn;
import com.jiubanqingchen.framework.annotation.SqlKey;
import com.jiubanqingchen.framework.base.service.ModelService;
import com.jiubanqingchen.framework.base.user.IUser;
import com.jiubanqingchen.framework.exception.WishException;
import com.jiubanqingchen.framework.security.AuthService;
import com.jiubanqingchen.framework.support.EncryptionSupport;
import com.jiubanqingchen.kit.ApplicationKit;
import com.jiubanqingchen.model.models.User;
import com.jiubanqingchen.org.resourceAuthority.ResourceAuthorityService;
import com.jiubanqingchen.org.userDept.UserDeptService;
import com.jiubanqingchen.org.userGroupRole.UserGroupRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author light_dust
 * @since 2020/12/27 20:19
 */
@SqlKey("org_user.list")
@HideColumn("userPassword")
public class UserService extends ModelService<User> {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    public User info(IUser adminUser) {
        //获取用户的基本信息
        User user=findById(adminUser.getUserId(),new Kv().set("hideColumns","userPassword,createTime,createPerson,modifyTime,modifyPerson"));
        //获取用户的菜单信息(存放的是角色的菜单Id)
        user.put("menus", Aop.get(ResourceAuthorityService.class).getRoleListMenuResource(adminUser.getRoleList()));
        return user;
    }

    /**
     * 根据账号获取用户信息
     *
     * @param userAccount 用户账号
     * @return 如有账号 存在 返回用户信息 反之 为null
     */
    public User getUserForAccount(String userAccount) throws WishException {
        return whereFirst("userAccount=?", userAccount);
    }

    @Override
    public void afterDelete(User user, Record record, IUser _usr) throws WishException {
        //删除用户的同时删除 用户角色 用户部门相关信息
        String userId = user.getUserId();
        String userName = user.getUserName();

        //删除用户角色关联信息
        UserGroupRoleService userGroupRoleService = Aop.get(UserGroupRoleService.class);
        userGroupRoleService.deleteWhere("userGroupId=? and userGroupType='USER'", userId);
        logger.info("正在删除用户 [{}] ({}) 角色关联信息...", userName, userId);
        //删除用户部门关联信息
        UserDeptService userDeptService = Aop.get(UserDeptService.class);
        userDeptService.deleteWhere("userId=?", userId);
        logger.info("正在删除用户 [{}] ({}) 部门关联信息...", userName, userId);
        //删除用户缓存
        AuthService authService = Aop.get(AuthService.class);
        authService.deleteUserCache(userId);
        super.afterDelete(user, record, _usr);
    }

    public void uploadExcel(UploadFile uploadFile,IUser _usr){

    }

    @Override
    public void preInsert(User user, Record record, IUser _usr) {
        if(StrKit.isBlank(user.getUserPassword())){
            user.setUserPassword(EncryptionSupport.me().encrypPassword(ApplicationKit.me().getUserInitPassword()));
        }
        super.preInsert(user, record, _usr);
    }
}
